/* eslint-disable operator-linebreak */
// 操作树的方法
import { getTreeData, randomStr } from '@/util/util'
import { delTaskType, saveTaskType } from '@/api/portalTaskUi/taskType'
export default {
  methods: {
    // 获取新增的id
    getNewAddId(params) {
      return new Promise(resolve => {
        saveTaskType(params).then(res => {
          resolve(res.data.data)
        })
      })
    },
    // 创建默认表单数据
    createdefaultInfo({ nodeName }) {
      return {
        typeName: nodeName, // 类型名称
        typeDescribe: '', // 类型描述
        isAssess: '0', // 是否考核
        isValid: '0', // 是否启用
        isConfig: '1',
        sort: '' // 排序
      }
    },
    // 增加子节点
    async addChildNode() {
      const ramdonStr = randomStr(3)
      const defaultStr = '新增节点' + ramdonStr
      const currentNode = getTreeData(this.tableData, this.row.nodeId)
      const nodeInfo = {
        pid: this.row.nodeId,
        children: []
      }
      const params = Object.assign(
        this.createdefaultInfo({ nodeName: defaultStr }),
        nodeInfo,

      )
      params.tenantId = this.currentId
      console.log(this.currentId)
      const data = await this.getNewAddId(params)
      params.nodeId = data
      params.nodeName = defaultStr
      this.$refs.tree.append(params, currentNode)
      if (!this.expand.includes(currentNode.nodeId)) {
        this.expand.push(currentNode.nodeId)
      }
      this.$refs.tree.setCurrentKey(params.nodeId)
      this.rowClickFun(params)
    },
    // 增加同级节点
    async addSameLevelNode() {
      console.log(1)
      const ramdonStr = randomStr(3)
      const defaultStr = '新增节点' + ramdonStr
      const nodeInfo = {
        pid: '0',
        children: []
      }
      const params = Object.assign(
        this.createdefaultInfo({ nodeName: defaultStr }),
        nodeInfo
      )
      params.tenantId = this.currentId
      console.log(this.currentId)
      const data = await this.getNewAddId(params)
      params.nodeId = data
      params.nodeName = defaultStr
      // this.tableData.push(params);
      if (this.tableData[this.tableData.length - 1]) {
        const lastId = this.tableData[this.tableData.length - 1].nodeId
        const lastNode = this.$refs.tree.getNode(lastId)
        this.$refs.tree.insertAfter(params, lastNode)
        this.$refs.tree.setCurrentKey(params.nodeId)
        this.rowClickFun(params)
      } else {
        this.tableData.push(params)
        this.rowClickFun(params)
        this.$nextTick(() => {
          // 默认选中第一项目
          this.$refs.tree.setCurrentKey(this.tableData[0].nodeId)
        })
      }
    },
    // 删除节点
    delFun() {
      // nodeName
      this.$confirm(
        `此操作将删除${this.row.nodeName}任务类型, 是否继续?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        const params = {
          id: this.row.nodeId,
          tenantId: this.currentId
        }
        delTaskType(params).then(res => {
          if (res.data.code === 0) {
            this.$refs.tree.remove(this.row)
            // eslint-disable-next-line operator-linebreak
            // ? this.tableData
            //     : getTreeData(this.tableData, this.row.pid).children;

            const targetData =
              this.row.pid === '0'
                ? this.tableData
                : getTreeData(this.tableData, this.row.pid).children

            if (targetData[0]) {
              this.$refs.tree.setCurrentKey(targetData[0].nodeId)
              this.rowClickFun(targetData[0])
            } else {
              this.$refs.tree.setCurrentKey(null)
              this.$emit('change', [])
              // this.rowClickFun(targetData[0]);
            }
          }
        })
      })
    }
  }
}
